package com.hunt.service.impl;

import com.hunt.model.entity.LongHuDetail;
import com.hunt.model.entity.LongHuDetailPo;
import com.hunt.model.entity.LongHuPo;
import com.hunt.model.entity.LongHuPoResult;
import com.hunt.service.LongHuService;
import com.hunt.util.LongHuFieldMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
@Transactional
public class LongHuServiceImpl implements LongHuService {
    @Autowired
    private MongoTemplate LongHuMongoTemplate;
    @Autowired
    private MongoTemplate LongHuDetailMongoTemplate;
    @Override
    public LongHuPoResult getpo(String date,Integer type,Integer sort) {
        String sortfield= LongHuFieldMap.fieldmap.get(type);
        LongHuPoResult lpr=new LongHuPoResult();
        Query query = new Query();
        if(sort==0){
            query.with(new Sort(Sort.Direction.ASC,sortfield));
        }
        if(sort==1){
            query.with(new Sort(Sort.Direction.DESC,sortfield));
        }
        List<LongHuPo> lh = LongHuMongoTemplate.find(query, LongHuPo.class,date);
        lpr.setDate(date);
        lpr.setList(lh);
        return lpr;

    }

    @Override
    public LongHuPoResult getlatestpo(String date,Integer type,Integer sort) {
        String sortfield= LongHuFieldMap.fieldmap.get(type);
        Set<String> ss=LongHuMongoTemplate.getCollectionNames();
        List<String> list = new ArrayList<>(ss);
        Collections.sort(list);
        date=list.get(list.size()-1);
        LongHuPoResult lpr=new LongHuPoResult();
        Query query = new Query();
        if(sort==0){
            query.with(new Sort(Sort.Direction.ASC, sortfield));
        }
        if(sort==1){
            query.with(new Sort(Sort.Direction.DESC,sortfield));
        }
        List<LongHuPo> lh = LongHuMongoTemplate.find(query, LongHuPo.class, date);
        lpr.setDate(date);
        lpr.setList(lh);
        return lpr;

    }

    @Override
    public LongHuDetail getpodetail(String date, String code, String isThreeDay,String listingReason) {
        LongHuDetail lhd=new LongHuDetail();
        List<LongHuDetailPo>buylist=new LinkedList<>();
        List<LongHuDetailPo>selllist=new LinkedList<>();
        Query query = new Query();
        if(!"0".equals(listingReason)){
            query.addCriteria(Criteria.where("reason").is(listingReason));
        }
        query.addCriteria(Criteria.where("ts_code").regex(code));
        query.addCriteria(Criteria.where("isThreeDay").is(Integer.valueOf(isThreeDay)));
        List<LongHuDetailPo> lh =LongHuDetailMongoTemplate.find(query, LongHuDetailPo.class, date);
        for(LongHuDetailPo lp:lh) {
          if("0".equals(lp.getSide())){
              if(lp.getBuy().equals("NaN")){
                 lp.setBuy("0.00");
              }
              else {
                  lp.setBuy(new BigDecimal(lp.getBuy()).setScale(2, RoundingMode.HALF_UP).toString());
              }

              if(lp.getBuy_rate().equals("NaN")){
                 lp.setSell_rate("0.00");
              }
              else {
                  lp.setBuy_rate(new BigDecimal(lp.getBuy_rate()).setScale(2, RoundingMode.HALF_UP).toString());
              }
              if(lp.getSell().equals("NaN")){
                  lp.setSell("0.00");
              }else {
                  lp.setSell(new BigDecimal(lp.getSell()).setScale(2, RoundingMode.HALF_UP).toString());;
              }
              if(lp.getSell_rate().equals("NaN")){
               lp.setSell_rate("0.00");
              }else {
                  lp.setSell_rate(new BigDecimal(lp.getSell_rate()).setScale(2, RoundingMode.HALF_UP).toString());
              }
              if(lp.getNet_buy().equals("NaN")){
                  lp.setNet_buy("0.00");
              }
              else {
                  lp.setNet_buy(new BigDecimal(lp.getNet_buy()).setScale(2, RoundingMode.HALF_UP).toString());
              }

            buylist.add(lp);
          }
          if("1".equals(lp.getSide())){
              if(lp.getBuy().equals("NaN")){
                  lp.setBuy("0.00");
              }
              else {
                  lp.setBuy(new BigDecimal(lp.getBuy()).setScale(2, RoundingMode.HALF_UP).toString());
              }

              if(lp.getBuy_rate().equals("NaN")){
                  lp.setSell_rate("0.00");
              }
              else {
                  lp.setBuy_rate(new BigDecimal(lp.getBuy_rate()).setScale(2, RoundingMode.HALF_UP).toString());
              }
              if(lp.getSell().equals("NaN")){
                  lp.setSell("0.00");
              }else {
                  lp.setSell(new BigDecimal(lp.getSell()).setScale(2, RoundingMode.HALF_UP).toString());;
              }
              if(lp.getSell_rate().equals("NaN")){
                  lp.setSell_rate("0.00");
              }else {
                  lp.setSell_rate(new BigDecimal(lp.getSell_rate()).setScale(2, RoundingMode.HALF_UP).toString());
              }
              if(lp.getNet_buy().equals("NaN")){
                  lp.setNet_buy("0.00");
              }
              else {
                  lp.setNet_buy(new BigDecimal(lp.getNet_buy()).setScale(2, RoundingMode.HALF_UP).toString());
              }
               selllist.add(lp);
            }
        }
//        Collections.sort(buylist, new Comparator<LongHuDetailPo>() {
//            @Override
//            public int compare(LongHuDetailPo o1, LongHuDetailPo o2) {
//                return (o1.getBuy()>o2.getBuy() ? -1 : (o1.getBuy().equals(o2.getBuy()) ? 0 : 1));
//            }
//        });
//        Collections.sort(selllist, new Comparator<LongHuDetailPo>() {
//            @Override
//            public int compare(LongHuDetailPo o1, LongHuDetailPo o2) {
//                return (o1.getSell()>o2.getSell() ? -1 : (o1.getSell().equals(o2.getSell()) ? 0 : 1));
//            }
//        });
        lhd.setBuylist(buylist);
        lhd.setSelllist(selllist);
        return lhd;
    }

    @Override
    public LongHuDetail getlatestpodetail(String date, String code, String isThreeDay,String listingReason) {
        Set<String> ss=LongHuDetailMongoTemplate.getCollectionNames();
        List<String> list = new ArrayList<>(ss);
        Collections.sort(list);
        date=list.get(list.size()-1);
        LongHuDetail lhd=new LongHuDetail();
        List<LongHuDetailPo>buylist=new LinkedList<>();
        List<LongHuDetailPo>selllist=new LinkedList<>();
        Query query = new Query();
        if(!"0".equals(listingReason)){
            query.addCriteria(Criteria.where("reason").is(listingReason));
        }
        query.addCriteria(Criteria.where("ts_code").regex(code));
        query.addCriteria(Criteria.where("isThreeDay").is(Integer.valueOf(isThreeDay)));
        List<LongHuDetailPo> lh =LongHuDetailMongoTemplate.find(query, LongHuDetailPo.class, date);
        for(LongHuDetailPo lp:lh) {

            if("0".equals(lp.getSide())){
                if(lp.getBuy().equals("NaN")){
                    lp.setBuy("0.00");
                }
                else {
                    lp.setBuy(new BigDecimal(lp.getBuy()).setScale(2, RoundingMode.HALF_UP).toString());
                }

                if(lp.getBuy_rate().equals("NaN")){
                    lp.setSell_rate("0.00");
                }
                else {
                    lp.setBuy_rate(new BigDecimal(lp.getBuy_rate()).setScale(2, RoundingMode.HALF_UP).toString());
                }
                if(lp.getSell().equals("NaN")){
                    lp.setSell("0.00");
                }else {
                    lp.setSell(new BigDecimal(lp.getSell()).setScale(2, RoundingMode.HALF_UP).toString());;
                }
                if(lp.getSell_rate().equals("NaN")){
                    lp.setSell_rate("0.00");
                }else {
                    lp.setSell_rate(new BigDecimal(lp.getSell_rate()).setScale(2, RoundingMode.HALF_UP).toString());
                }
                if(lp.getNet_buy().equals("NaN")){
                    lp.setNet_buy("0.00");
                }
                else {
                    lp.setNet_buy(new BigDecimal(lp.getNet_buy()).setScale(2, RoundingMode.HALF_UP).toString());
                }

                buylist.add(lp);
            }
            if("1".equals(lp.getSide())){
                if(lp.getBuy().equals("NaN")){
                    lp.setBuy("0.00");
                }
                else {
                    lp.setBuy(new BigDecimal(lp.getBuy()).setScale(2, RoundingMode.HALF_UP).toString());
                }

                if(lp.getBuy_rate().equals("NaN")){
                    lp.setSell_rate("0.00");
                }
                else {
                    lp.setBuy_rate(new BigDecimal(lp.getBuy_rate()).setScale(2, RoundingMode.HALF_UP).toString());
                }
                if(lp.getSell().equals("NaN")){
                    lp.setSell("0.00");
                }else {
                    lp.setSell(new BigDecimal(lp.getSell()).setScale(2, RoundingMode.HALF_UP).toString());;
                }
                if(lp.getSell_rate().equals("NaN")){
                    lp.setSell_rate("0.00");
                }else {
                    lp.setSell_rate(new BigDecimal(lp.getSell_rate()).setScale(2, RoundingMode.HALF_UP).toString());
                }
                if(lp.getNet_buy().equals("NaN")){
                    lp.setNet_buy("0.00");
                }
                else {
                    lp.setNet_buy(new BigDecimal(lp.getNet_buy()).setScale(2, RoundingMode.HALF_UP).toString());
                }
                selllist.add(lp);
            }
        }
        lhd.setBuylist(buylist);
        lhd.setSelllist(selllist);
        return lhd;
    }
}
